package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.service.INursingPlanService;
import com.zzyl.nursing.service.INursingProjectPlanService;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 护理计划Service业务层处理
 * 
 * @author zzyl
 * @date 2025-02-20
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private INursingProjectPlanService inursingProjectPlanService;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        NursingPlan nursingPlan = this.getById(id);
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        List<NursingProjectPlan> list = inursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        List<NursingProjectPlanVo> nursingProjectPlanVos = BeanUtil.copyToList(list, NursingProjectPlanVo.class);
        nursingPlanVo.setProjectPlans(nursingProjectPlanVos);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        //mybatisPlus自动主键回填
        boolean flag = this.save(nursingPlan);
        if (!flag) {
            throw new BaseException("新增护理计划的部门失败");
        }
        List<NursingProjectPlan> list = nursingPlanDto.getProjectPlans();
        list.forEach(v -> v.setPlanId(nursingPlan.getId()));
        flag = inursingProjectPlanService.saveBatch(list);
        if (!flag) {
            throw new BaseException("新增护理计划的护理项目部分失败");
        }
        return 1;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        if (ObjectUtil.isNull(nursingPlanDto)) {
            throw new BaseException("参数不能为空");
        }
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        boolean flag = this.updateById(nursingPlan);
        if (!flag) {
            throw new BaseException("护理计划存储异常");
        }
        flag = inursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery()
                .eq(NursingProjectPlan::getPlanId, nursingPlanDto.getId()));
        if (!flag) {
            throw new BaseException("修改护理计划的删除护理项目异常");
        }
        List<NursingProjectPlan> list = nursingPlanDto.getProjectPlans();
        list.forEach(v -> v.setPlanId(nursingPlan.getId()));
        flag = inursingProjectPlanService.saveBatch(list);
        if (!flag) {
            throw new BaseException("新增护理计划的护理项目部分失败");
        }
        return 1;
    }

    /**
     * 批量删除护理计划
     * 
     * @param id 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteNursingPlanByIds(Long id)
    {
        if (ObjectUtil.isEmpty(id)) {
            throw new BaseException("参数不能为空");
        }
        boolean flag = inursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        if (!flag) {
            throw new BaseException("删除护理计划的删除护理项目异常");
        }
        flag = removeById(id);
        if (!flag) {
            throw new BaseException("删除护理计划的删除护理计划异常");
        }
        return 1;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        // return nursingPlanMapper.deleteNursingPlanById(id);
        return this.removeById(id) ? 1 : 0;
    }

    @Override
    public List<NursingPlan> selectAll() {
        return list();
    }
}
